package com.ehome.poct.core.dao.mybatis;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.mybatisplus.support.AbstractCrudRepository;
import com.ehome.poct.core.api.dto.check.gold.TestConfPoctAnalysisDTO;
import com.ehome.poct.core.api.vo.checkdata.TestDataDetailVO;
import com.ehome.poct.core.dao.entity.check.TestConfPoctAnalysisDO;
import com.ehome.poct.core.dao.entity.check.TestConfPoctUniAnalysisDO;
import com.ehome.poct.core.dao.mapping.CheckReportMapping;
import com.ehome.poct.core.dao.mybatis.mapper.TestConfPoctAnalysisMapper;
import com.ehome.poct.core.dao.mybatis.mapper.TestConfPoctUniAnalysisMapper;
import com.ehome.poct.core.repository.TestConfPoctAnalysisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 金标仪 检测报告解析的病情的判读术语配置表RepositoryImpl
 *
 * @author muqiu
 * @Date 2022-03-19 16:56
 */
@Repository
public class TestConfPoctAnalysisRepositoryImpl extends AbstractCrudRepository<TestConfPoctAnalysisDO, TestConfPoctAnalysisDTO> implements TestConfPoctAnalysisRepository {

    @Autowired
    private TestConfPoctAnalysisMapper testConfPoctAnalysisMapper;
    @Autowired
    private TestConfPoctUniAnalysisMapper testConfPoctUniAnalysisMapper;
    @Autowired
    private CheckReportMapping checkReportMapping;

    @Override
    public TestConfPoctAnalysisDTO getAnalysisByData(TestDataDetailVO data, Integer isChild) {
        if (data.isMultiValue()) {
            return checkReportMapping.to(this.getAnalysisByOne(data, isChild));
        } else {
            //定制化操作  crp saa  标线点 取左区间
            //if (data.getStripTypeCode() == 4 || data.getStripTypeCode() == 5) {
            //    return BeanUtil.toBean(testConfPoctAnalysisMapper.selectOne(Wrappers.<TestConfPoctAnalysisDO>lambdaQuery()
            //            .eq(TestConfPoctAnalysisDO::getStripTypeCode, data.getStripTypeCode())
            //            .eq(TestConfPoctAnalysisDO::getStripBatch, data.getStripBatch())
            //            .lt(TestConfPoctAnalysisDO::getMin, data.getValue1())
            //            .ge(TestConfPoctAnalysisDO::getMax, data.getValue1())
            //            .eq(TestConfPoctAnalysisDO::getPatientType, isChild)
            //    ), TestConfPoctAnalysisDTO.class);
            //}

            // 甲乙流特殊处理 jira-HARDSOFT-1534
            //if (StripTypeEnums.FLUA_AG.getStripTypeCode().equals(data.getStripTypeCode())) {
            //    return BeanUtil.toBean(testConfPoctAnalysisMapper.selectOne(Wrappers.<TestConfPoctAnalysisDO>lambdaQuery()
            //            .eq(TestConfPoctAnalysisDO::getStripTypeCode, data.getStripTypeCode())
            //            .eq(TestConfPoctAnalysisDO::getStripBatch, data.getStripBatch())
            //            .le(TestConfPoctAnalysisDO::getMin, data.getValue1())
            //            .ge(TestConfPoctAnalysisDO::getMax, data.getValue1())
            //            .eq(TestConfPoctAnalysisDO::getPatientType, isChild)
            //    ), TestConfPoctAnalysisDTO.class);
            //}

            TestConfPoctAnalysisDO poctAnalysisDO = testConfPoctAnalysisMapper.selectOne(Wrappers.<TestConfPoctAnalysisDO>lambdaQuery()
                    .eq(TestConfPoctAnalysisDO::getStripTypeCode, data.getStripTypeCode())
                    .eq(TestConfPoctAnalysisDO::getStripBatch, data.getStripBatch())
                    .le(TestConfPoctAnalysisDO::getLogicMinValue, data.getValue1())
                    .gt(TestConfPoctAnalysisDO::getLogicMaxValue, data.getValue1())
                    .eq(TestConfPoctAnalysisDO::getPatientType, isChild).last(" limit 1"));

            if (Objects.isNull(poctAnalysisDO)) {
                return null;
            }
            return BeanUtil.toBean(poctAnalysisDO, TestConfPoctAnalysisDTO.class);
        }
    }

    private TestConfPoctUniAnalysisDO getAnalysisByOne(TestDataDetailVO data, Integer isChild) {
        List<TestConfPoctUniAnalysisDO> analysisList = this.testConfPoctUniAnalysisMapper.selectList(Wrappers.<TestConfPoctUniAnalysisDO>lambdaQuery()
                .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeA, data.getStripTypeCode())
                .le(TestConfPoctUniAnalysisDO::getStripTypeAMin, data.getValue1())
                .gt(TestConfPoctUniAnalysisDO::getStripTypeAMax, data.getValue1())
                .eq(TestConfPoctUniAnalysisDO::getStripTypeCodeB, data.getStripTypeCode())
                .le(TestConfPoctUniAnalysisDO::getStripTypeBMin, data.getValue2())
                .gt(TestConfPoctUniAnalysisDO::getStripTypeBMax, data.getValue2())
                .eq(TestConfPoctUniAnalysisDO::getPatientType, isChild)
        );
        // 通过试纸批次过滤解读
        return analysisList.parallelStream()
                .filter(i -> CollUtil.contains(Arrays.asList(i.getStripTypeABatch().split(",")), data.getStripBatch().toString()))
                .filter(i -> CollUtil.contains(Arrays.asList(i.getStripTypeBBatch().split(",")), data.getStripBatch().toString()))
                .findFirst().orElse(null);
    }
}
